package cn.edu.ujn.boot.controller;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.catalina.connector.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.edu.ujn.boot.po.Customer;
import cn.edu.ujn.boot.po.User;
import cn.edu.ujn.boot.service.ICustomerService;

@Controller
@RequestMapping("/customer")
public class CustomerController {
	@Autowired
	private ICustomerService customerService;

	/**
	 * 导入jackson包。
	 * 
	 * @param pn
	 * @return
	 */
	@RequestMapping(value = "/custUpdate/{id}", method = RequestMethod.GET)
	public String updateCustomer(Customer customer, @PathVariable("id") Integer custId) {
		customer.setCustId(custId);
		int keySelective = customerService.updateByPrimaryKeySelective(customer);
		return "customer";
	}

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public String deleteCustomer(Customer customer, @PathVariable("id") Integer custId) {
		int deleteByPrimaryKey = customerService.deleteByPrimaryKey(custId);
		return "customer";
	}
	
	/**
	 * 接收条件查询的数据，将结果通过分页查询输出
	 * @param pn
	 * @param model
	 * @param customer
	 * @return
	 */
	@RequestMapping(value = "list", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	public String getEmp(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model, Customer customer) {
		// 这不是一个分页查询
		// 引入PageHelper分页插件
		// 在查询之前只需要调用，传入页码，以及每页的大小

		PageHelper.startPage(pn, 5); 
		// startPage后面紧跟的这个查询就是一个分页查询
		List<Customer> cust = customerService.selectSelective(customer);
		// 使用pageInfo包装查询后的结果，由pageInfo交给页面
		// 封装了详细的分页信息,包括查询的数据，传入连续显示的页数

		PageInfo page = new PageInfo(cust, 5);
		model.addAttribute("pageInfo", page);

		return "customer";
	}

	@RequestMapping(value = "/source", method = RequestMethod.POST)
	public String saveCustomer(Customer customer) {
		System.out.println(customer.getCustName());
		int insert = customerService.insert(customer);

		return "customer";
	}

	/**
	 * 从数据库查询所有客户信息，并通过分页查询提交给customer页面
	 * 
	 * @param pn
	 * @param model
	 * @return
	 */
	@RequestMapping("/lists")
	public String getLists(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {
		// 引入PageHelper分页插件
		// 在查询之前只需要调用，传入页码，以及每页的大小
		PageHelper.startPage(pn, 5); 
		// startPage后面紧跟的这个查询就是一个分页查询
		List<Customer> cust = customerService.selectByExample();

		// 使用pageInfo包装查询后的结果，由pageInfo交给页面
		// 封装了详细的分页信息,包括有我们查询出来的数据，传入连续显示的页数
		PageInfo page = new PageInfo(cust, 5);
		model.addAttribute("pageInfo", page);

		return "customer";
	}

}
